package com.gy.bearhelps.home;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.AbsListView;
import android.widget.ListView;
import android.widget.TextView;

import com.gy.bearhelps.R;
import com.gy.bearhelps.base.BaseActivity;
import com.gy.bearhelps.home.adapter.YMSJAdapter;
import com.gy.bearhelps.home.bean.YMSJBean;
import com.gy.bearhelps.home.util.CJSJDataManager;
import com.gy.bearhelps.util.ListViewItemListener;
import com.gy.bearhelps.util.SharedPrefUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 高岳 on 2016/11/4.
 * Describe:疫苗时间
 */
public class YMSJActivity extends BaseActivity implements ListViewItemListener {

    private TextView tvStatus;
    private ListView listView;
    private YMSJAdapter adapter;
    private List<YMSJBean> lists,showLists,normalList,finishList;
    private List<Integer> selects;
    private String strSelect;

    @Override
    protected void initView() {
        setContentViewRes(R.layout.activity_ymsj);
        setTitleMsg("疫苗时间表");
        tvStatus = (TextView) findViewById(R.id.activity_cjsj_tv_status);
        listView = (ListView) findViewById(R.id.activity_cjsj_listview);
    }

    @Override
    protected void initData() {
        lists = new ArrayList<>();
        showLists = new ArrayList<>();
        normalList = new ArrayList<>();
        finishList = new ArrayList<>();
        selects = new ArrayList<>();
        lists = CJSJDataManager.getYMSJData(this);
        showLists.addAll(lists);
        adapter = new YMSJAdapter(this,showLists);
        adapter.setListener(this);
        strSelect = SharedPrefUtil.get("ymsj","");
        if(!TextUtils.isEmpty(strSelect)){
            String[] strs = strSelect.split(",");
            for (String str : strs){
                selects.add(Integer.valueOf(str));
            }
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                tvStatus.setText(showLists.get(firstVisibleItem).title+"");
            }
        });
    }

    @Override
    protected void fillData() {
        super.fillData();
        listView.setAdapter(adapter);
        if(selects.size() > 0){
//            handler.sendEmptyMessageDelayed(0,500);
            handleData();
        }
    }

    @Override
    public void doPassActionListener(Object obj, int org1, int org2, String str) {
        if(showLists.get(org1).status == 0){
            selects.add(showLists.get(org1).id);
            showLists.get(org1).status = 1;
        } else {
            selects.remove(Integer.valueOf(showLists.get(org1).id));
            showLists.get(org1).status = 0;
        }
        handleData();
    }

    /** 筛选数据*/
    private void handleData(){
        for (YMSJBean newBean : showLists){
            if(selects.contains(newBean.id)){
                newBean.status = 1;
                finishList.add(newBean);
            } else {
                newBean.status = 0;
                normalList.add(newBean);
            }
        }
        adapter.notifyDataSetChanged();

        String saveSelect = "";
        for (int i : selects){
            saveSelect += i +",";
        }
        if(!TextUtils.isEmpty(saveSelect)){
            saveSelect = saveSelect.substring(0,saveSelect.length() - 1);
        }
        SharedPrefUtil.put("ymsj",saveSelect);
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            handleData();
        }
    };
}
